<template>
  <div class="post-complaint-container">
    <div class="complaint-content">
      <!-- 顶部标题区域 - 根据图片调整 -->
      <div class="header-section">
        <div class="title-wrapper">
          
          <div class="title-icon">
            <Icon icon="twemoji:disappointed-face" width="24" height="24" color="#fff" />
          </div>
          
          <h1 class="page-title">发布吐槽帖子</h1>
        </div>
        <p class="page-subtitle">分享你的职场经历，让更多人了解真实的工作环境</p>
      </div>

      <div> <hr /></div>

      <!-- 吐槽内容区域 -->
      <div class="content-section">
        <div class="section-header">
          <div class="section-icon">
            <el-icon><Edit /></el-icon>
          </div>
          <h3 class="section-title">吐槽内容</h3>
        </div>
        
        <!-- 吐槽标题 -->
        <div class="form-item">
          <div class="form-label">
            <span class="label-text">吐槽标题</span>
            <span class="required-star">*</span>
          </div>
          <div class="input-wrapper">
            <el-input
              v-model="formData.title"
              placeholder="用一句话概括你的吐槽"
              maxlength="100"
              show-word-limit
              class="custom-input"
            />
          </div>
        </div>

        <!-- 详细内容 -->
        <div class="form-item">
          <div class="form-label">
            <span class="label-text">详细内容</span>
            <span class="required-star">*</span>
          </div>
          <div class="input-wrapper">
            <el-input
              v-model="formData.content"
              type="textarea"
              :rows="6"
              placeholder="详细描述你遇到的问题，分享你的真实经历..."
              maxlength="500"
              show-word-limit
              resize="none"
              class="custom-textarea"
            />
          </div>
        </div>
      </div>

      <!-- 分类信息区域 -->
      <div class="category-section">
        <div class="section-header">
          <div class="section-icon">
            <el-icon><Folder /></el-icon>
          </div>
          <h3 class="section-title">分类信息</h3>
        </div>
        <div class="category-row">
          <div class="form-item">
            <div class="form-label">
              <span class="label-text">吐槽分类</span>
              <span class="required-star">*</span>
            </div>
            <div class="input-wrapper">
              <el-select
                v-model="formData.category"
                placeholder="请选择吐槽分类"
                class="custom-select"
              >
                <el-option
                  v-for="item in categoryOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </div>

          <div class="form-item">
            <div class="form-label">
              <span class="label-text">吐槽公司</span>
              <span class="required-star">*</span>
            </div>
            <div class="input-wrapper">
              <el-select
                v-model="formData.company"
                placeholder="请选择公司"
                class="custom-select"
                filterable
              >
                <el-option
                  v-for="item in companyOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </div>
          </div>
        </div>
      </div>

      <!-- 自定义标签区域 -->
  
<div class="tags-section">
  <div class="section-header">
    <div class="section-icon">
      <el-icon><PriceTag /></el-icon>
    </div>
    <h3 class="section-title">自定义标签</h3>
  </div>
  
  <!-- 已选标签 -->
   <div class="selected-tags" v-if="formData.selectedTags.length > 0">
  <span class="tags-label">选择标签</span>
  <div class="tag-input-container">
    <div class="tag-container">
      <el-tag
        v-for="tag in formData.selectedTags"
        :key="tag"
        closable
        @close="removeTag(tag)"
        class="selected-tag"
        type="danger"
      >
        {{ tag }}
      </el-tag>
    </div>
  </div>
</div>

  <!-- 预设标签 -->
  <div class="preset-tags">
    <el-checkbox-group v-model="formData.presetTags" class="tags-checkbox-group">
      <el-checkbox
        v-for="tag in presetTagOptions"
        :key="tag"
        :label="tag"
        class="tag-checkbox"
      >
        {{ tag }}
      </el-checkbox>
    </el-checkbox-group>
  </div>

  <!-- 自定义标签输入 -->
  <div class="custom-tag-input">
    <el-input
      v-model="customTagInput"
      placeholder="输入自定义标签"
      class="tag-input"
    />
    <el-button type="primary" @click="addCustomTag" class="add-tag-btn">
      添加
    </el-button>
  </div>
</div>


      <!-- 发布设置区域 -->
      <div class="publish-settings">
        <div class="section-header">
          <div class="section-icon">
            <el-icon><Setting /></el-icon>
          </div>
          <h3 class="section-title">发布设置</h3>
        </div>
        
        <div class="anonymous-setting">
          <el-switch v-model="formData.anonymous" class="anonymous-switch" />
          <div class="anonymous-text">
            <span class="anonymous-label">匿名发布</span>
            <p class="anonymous-desc">选择匿名发布可以保护您的隐私</p>
          </div>
        </div>
        
      
      </div>

    

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button class="cancel-btn" size="large" @click="handleCancel">取消</el-button>
       
        <el-button type="primary" class="publish-btn" size="large" @click="handleSubmit" :loading="submitLoading">发布吐槽</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Icon } from '@iconify/vue'
import { reactive, ref, watch, onMounted } from 'vue'
import { 
  Edit, 
  Folder, 
  PriceTag, 
  Setting, 
  Picture, 
  Plus 
} from '@element-plus/icons-vue'
import { RoastApi } from '@/utils/api'
import { CampatydataApi } from '@/utils/api'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据
const formData = reactive({
  title: '',
  content: '',
  category: '',
  company: '',
  selectedTags: [],
  presetTags: [],
  anonymous: true
})

const customTagInput = ref('')
const companyOptions = ref([])
const submitLoading = ref(false)

// 选项数据
const categoryOptions = [
  { label: '工作环境', value: '工作环境' },
  { label: '薪资福利', value: '薪资福利' },
  { label: '管理制度', value: '管理制度' },
  { label: '人际关系', value: '人际关系' },
  { label: '职业发展', value: '职业发展' },
  { label: '其他', value: '其他' }
]

const presetTagOptions = [
  '加班严重', '工资拖欠', '管理混乱', '勾心斗角', '形式主义', '官僚主义'
]

// 方法
const removeTag = (tag) => {
  const index = formData.selectedTags.indexOf(tag)
  if (index > -1) {
    formData.selectedTags.splice(index, 1)
    
    // 同时从预设标签中移除
    const presetIndex = formData.presetTags.indexOf(tag)
    if (presetIndex > -1) {
      formData.presetTags.splice(presetIndex, 1)
    }
  }
}

const addCustomTag = () => {
  if (customTagInput.value.trim() && !formData.selectedTags.includes(customTagInput.value.trim())) {
    formData.selectedTags.push(customTagInput.value.trim())
    customTagInput.value = ''
  }
}

const handleImageChange = (file) => {
  console.log('文件变更:', file)
}

// 监听预设标签变化
watch(() => formData.presetTags, (newTags) => {
  // 合并已选标签和预设标签，去重
  formData.selectedTags = [...new Set([...formData.selectedTags, ...newTags])]
})

// 获取公司列表
const fetchCompanies = async () => {
  try {
    const response = await CampatydataApi.getCampatyDataList()
    // 根据实际返回结构调整
    if (Array.isArray(response)) {
      companyOptions.value = response
    } else if (response.data) {
      companyOptions.value = response.data.items || response.data
    } else {
      companyOptions.value = response.items || response
    }
  } catch (error) {
    console.error('获取公司列表失败:', error)
    ElMessage.error('获取公司列表失败')
  }
}

// 提交表单
const handleSubmit = async () => {
  submitLoading.value = true
  try {
    const submitData = {
      title: formData.title,
      content: formData.content,
      is_anonymous: formData.anonymous,
      category: formData.category,
      company_id: formData.company,
      custom_tags: formData.selectedTags
    }
    
    await RoastApi.submitRoast(submitData)
    ElMessage.success('发布成功')
    router.push('/roastlist')
  } catch (error) {
    console.error('发布失败:', error)
    // 显示更详细的错误信息
    let errorMsg = '发布失败'
    if (error.response && error.response.data && error.response.data.detail) {
      errorMsg += ': ' + error.response.data.detail
    } else if (error.message) {
      errorMsg += ': ' + error.message
    }
    ElMessage.error(errorMsg)
  } finally {
    submitLoading.value = false
  }
}

// 取消操作
const handleCancel = () => {
  router.go(-1)
}

// 组件挂载时获取公司列表
onMounted(() => {
  fetchCompanies()
})
</script>

<style scoped>
.post-complaint-container {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 20px 0;
}

.complaint-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
  background:white;
  border-radius: 8px;
}

/* 顶部标题区域样式 - 根据图片调整 */
.header-section {
  background: white;
  padding: 20px 24px;
  border-radius: 8px;
  margin-bottom: 20px;
  text-align: left; /* 改为左对齐 */
}

.title-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px; /* 减小间距 */
}

.title-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}



.cross-icon {
  color: white;
  font-size: 18px;
  font-weight: bold;
  line-height: 1;
}

.page-title {
  font-size: 18px; /* 减小字体大小 */
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

.page-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
  line-height: 1.5;
  margin-left: 36px; /* 与标题对齐 */
}

/* 区块标题样式 */
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  padding-bottom: 12px;

}

.section-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ff4757;
  font-size: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

/* 内容区块通用样式 */
.content-section,
.category-section,
.tags-section,
.publish-settings,
.upload-section {
  background: white;
  padding: 24px;
  border-radius: 8px;
 
  
}

/* 表单项样式 */
.form-item {
  margin-bottom: 24px;
}

.form-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.label-text {
  font-size: 14px;
  font-weight: 500;
  color: #1a1a1a;
}

.required-star {
  color: #ff4757;
  margin-left: 4px;
}

.input-wrapper {
  width: 100%;
}

/* 分类信息布局 */
.category-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

/* 输入框样式 */
:deep(.custom-input .el-input__wrapper) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-shadow: none;
  padding: 12px;
  transition: all 0.3s ease;
}

:deep(.custom-input .el-input__wrapper:hover) {
  border-color: #c0c4cc;
}

:deep(.custom-input .el-input__wrapper.is-focus) {
  border-color: #ff4757;
  box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.1);
}

:deep(.custom-input .el-input__inner) {
  font-size: 14px;
  color: #1a1a1a;
}

/* 文本域样式 */
:deep(.custom-textarea .el-textarea__inner) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-shadow: none;
  padding: 12px;
  font-size: 14px;
  line-height: 1.5;
  color: #1a1a1a;
  resize: vertical;
  min-height: 120px;
  transition: all 0.3s ease;
}

:deep(.custom-textarea .el-textarea__inner:focus) {
  border-color: #ff4757;
  box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.1);
}

/* 选择器样式 */
:deep(.custom-select .el-select__wrapper) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-shadow: none;
  padding: 1px 11px;
  height: 40px;
}

:deep(.custom-select .el-select__wrapper:hover) {
  border-color: #c0c4cc;
}

:deep(.custom-select .el-select__wrapper.is-focus) {
  border-color: #ff4757;
  box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.1);
}

/* 标签区域样式 */

.selected-tags {
  margin-bottom: 20px;
}

.tags-label {
  font-size: 14px;
  color: #666;
  display: block;
  margin-bottom: 8px;
}
.tag-input-container {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 8px 12px;
  margin-bottom: 12px;
  background-color: #fff;
}

.tag-container {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.selected-tag {
  margin-right: 8px;
  margin-bottom: 8px;
  background: #409eff;
  border: none;
  color: white;
  border-radius: 12px;
  padding: 4px 8px;
  font-size: 12px;
}

:deep(.selected-tag .el-tag__close) {
  color: white;
  margin-left: 4px;
  font-size: 12px;
}

:deep(.selected-tag .el-tag__close:hover) {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
}


.preset-tags {
  margin-bottom: 20px;
}

.tags-checkbox-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}

.tag-checkbox {
  margin-right: 0;
}

:deep(.tag-checkbox .el-checkbox__label) {
  font-size: 14px;
}

.custom-tag-input {
  display: flex;
  gap: 12px;
  align-items: center;
}

.tag-input {
  flex: 1;
}

.add-tag-btn {
  white-space: nowrap;
}

/* 发布设置样式 */
.anonymous-setting {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.anonymous-text {
  flex: 1;
}

.anonymous-label {
  font-size: 14px;
  font-weight: 500;
  color: #1a1a1a;
  display: block;
  margin-bottom: 4px;
}

.anonymous-desc {
  font-size: 12px;
  color: #666;
  margin: 0;
  line-height: 1.4;
}

.anonymous-identity {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 4px;
  border-left: 3px solid #409eff;
}

.identity-name {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 4px 0;
}

.identity-desc,
.identity-note {
  font-size: 12px;
  color: #666;
  margin: 2px 0;
  line-height: 1.4;
}

/* 上传区域样式 */
:deep(.image-uploader .el-upload) {
  width: 100%;
  border: 2px dashed #dcdfe6;
  border-radius: 6px;
  background: #fafafa;
  transition: all 0.3s ease;
}

:deep(.image-uploader .el-upload:hover) {
  border-color: #409eff;
}

.upload-content {
  padding: 40px 20px;
  text-align: center;
  color: #666;
}

.upload-icon {
  font-size: 24px;
  color: #c0c4cc;
  margin-bottom: 8px;
}

.upload-hint {
  font-size: 12px;
  color: #999;
  margin: 8px 0 0 0;
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 30px 0;
}

.cancel-btn,
.preview-btn {
  width: 100px;
  border: 1px solid #dcdfe6;
  color: #666;
  font-weight: 500;
}

.cancel-btn:hover {
  border-color: #409eff;
  color: #409eff;
  background: rgba(71, 83, 255, 0.05);
}

.preview-btn:hover {
  border-color: #409eff;
  color: #409eff;
  background: rgba(64, 158, 255, 0.05);
}

.publish-btn {
  width: 120px;
  background: #409eff;
  border: none;
  font-weight: 500;
}

.publish-btn:hover {
  background: #409eff;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(74, 71, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .complaint-content {
    padding: 0 16px;
  }
  
  .category-row {
    grid-template-columns: 1fr;
    gap: 0;
  }
  
  .tags-checkbox-group {
    grid-template-columns: 1fr 1fr;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .cancel-btn,
  .preview-btn,
  .publish-btn {
    width: 100%;
  }
}
</style>